from django.db import models
from django.contrib.auth.models import User

# ORM 模型
# Create your models here.
class Topic(models.Model):
    """用户学习的主题"""

    text = models.CharField(max_length=200)
    date_added = models.DateTimeField(auto_now_add=True)
    ower = models.ForeignKey(User)

    def __str__(self):
        return "<Topic.text>"+self.text

class Entry(models.Model):
    """学到的有关某个主题的具体知识"""

    topic = models.ForeignKey(Topic) #设置了外键 那么在Topic会自动生成一个 entry_set 属性 名称规则就是Entry的小写加_set
    text = models.TextField()
    date_added = models.DateField(auto_now_add=True) 
    
    class Meta:
        verbose_name_plural = "entries" #指定复数形式的名称 不指定自动在类名后面加s

    def __str__(self):
        return "<Entry.text>" + self.text[:50]


class Comment(models.Model):
    ower = models.ForeignKey(User)
    entry = models.ForeignKey(Entry) # entry.comment_set.all() 就可以获取到所有评论
    text = models.TextField()
    date_added = models.DateTimeField(auto_now_add=True)

    def __str__(self):
        return "<Comment>:"+self.ower.username+"->"+self.text
